﻿namespace Pub.Class
{
    using System;
    using System.IO.Ports;
    using System.Threading;

    public class COMPort
    {
        public SerialPort port;
        public bool ReceiveEventFlag;

        public event SerialErrorReceivedEventHandler Error;

        public event PortDataReceivedEventHandle Received;

        public COMPort(string sPortName, COMBaudRates baudrate, Parity parity)
        {
            this.port = new SerialPort(sPortName, (int) baudrate, parity, 8, StopBits.One);
            this.port.RtsEnable = true;
            this.port.ReadTimeout = 0xbb8;
            this.port.DataReceived += new SerialDataReceivedEventHandler(this.DataReceived);
            this.port.ErrorReceived += new SerialErrorReceivedEventHandler(this.ErrorEvent);
        }

        public COMPort(string sPortName, COMBaudRates baudrate, Parity parity, COMDataBits databits, StopBits stop)
        {
            this.port = new SerialPort(sPortName, (int) baudrate, parity, (int) databits, stop);
            this.port.RtsEnable = true;
            this.port.ReadTimeout = 0xbb8;
            this.port.DataReceived += new SerialDataReceivedEventHandler(this.DataReceived);
            this.port.ErrorReceived += new SerialErrorReceivedEventHandler(this.ErrorEvent);
        }

        public void Close()
        {
            if (this.port.IsOpen)
            {
                this.port.Close();
            }
        }

        public void DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (!this.ReceiveEventFlag)
            {
                byte[] buffer = new byte[this.port.BytesToRead];
                this.port.Read(buffer, 0, buffer.Length);
                if (this.Received.IsNotNull())
                {
                    this.Received(sender, new PortDataReciveEventArgs(buffer));
                }
            }
        }

        public void ErrorEvent(object sender, SerialErrorReceivedEventArgs e)
        {
            if (this.Error.IsNotNull())
            {
                this.Error(sender, e);
            }
        }

        ~COMPort()
        {
            this.Close();
        }

        public bool IsOpen()
        {
            return this.port.IsOpen;
        }

        public void Open()
        {
            if (!this.port.IsOpen)
            {
                this.port.Open();
            }
        }

        public int SendCommand(byte[] SendData, ref byte[] ReceiveData, int Overtime)
        {
            if (!this.port.IsOpen)
            {
                return -1;
            }
            this.ReceiveEventFlag = true;
            this.port.DiscardInBuffer();
            this.port.Write(SendData, 0, SendData.Length);
            int num = 0;
            int num2 = 0;
            while (num++ < Overtime)
            {
                if (this.port.BytesToRead >= ReceiveData.Length)
                {
                    break;
                }
                Thread.Sleep(1);
            }
            if (this.port.BytesToRead >= ReceiveData.Length)
            {
                num2 = this.port.Read(ReceiveData, 0, ReceiveData.Length);
            }
            this.ReceiveEventFlag = false;
            return num2;
        }

        public void SendData(byte[] data)
        {
            if (this.port.IsOpen)
            {
                this.port.Write(data, 0, data.Length);
            }
        }

        public void SendData(string data)
        {
            if (this.port.IsOpen)
            {
                this.port.Write(data);
            }
        }

        public void SendData(byte[] data, int offset, int count)
        {
            if (this.port.IsOpen)
            {
                this.port.Write(data, offset, count);
            }
        }
    }
}

